﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ApartmentManage.View;

namespace ApartmentManage
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ResidenceEntities db = new ResidenceEntities();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            string pass = this.txtPassword.Text.Trim();
            string id = this.txtUsername.Text.Trim();
            int f = 0;

            string firstname = "";
            string lastname = "";

            try
            {
                if (iResident.IsSelected == true)
                {
                    var rs = from r in db.Residents
                             where (r.residentNo == id && r.ID == pass)
                             select r;



                    if (rs.ToList().Count == 0)
                    {
                        f = 1;
                    }
                    else
                    {
                        var rl = rs.ToList();
                        firstname = rl.First().firstName;
                        lastname = rl.First().lastName;

                        ResidentView wd = new ResidentView();
                        wd.name = firstname + " " + lastname;
                        wd.Show();
                        this.Hide();
                    }
                }
                else
                {
                    var rs = from r in db.Staffs
                             where (r.staffNo == id && r.telephone == pass)
                             select r;



                    if (rs.ToList().Count == 0)
                    {
                        f = 1;
                    }
                    else
                    {
                        var rl = rs.ToList();
                        firstname = rl.First().firstName;
                        lastname = rl.First().lastName;

                        if (rl.First().position.Equals("supervisor"))
                        {
                            SupervisorView wd = new SupervisorView();
                            wd.name = firstname + " " + lastname;
                            wd.Show();
                            this.Hide();
                        }
                        else
                        {
                            MessageBox.Show("Service not available.");

                            this.txtPassword.Text = "";
                            this.txtUsername.Text = "";
                        }

                        
                    }
                }

                
                if(f==1)
                {
                    MessageBox.Show("Failed!!!");

                    this.txtPassword.Text = "";
                    this.txtUsername.Text = "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.ToString());
            }
        }
    }
}
